layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户场景列表
    var tableIns = table.render({
        elem: '#userList',
        url : '/user/list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'loginName', title: '用户名', minWidth:100, align:"center"},
            {field: 'email', title: '用户邮箱', minWidth:200, align:'center',
                templet:function(d){
                    return '<a class="layui-blue" href="mailto:'+d.email+'">'+(d.email || '')+'</a>';
                }
            },
            {field: 'sex', title: '性别', align:'center'},
            {field: 'loginFlag', title: '用户状态',  align:'center',
                templet:function(d){
                    return d.loginFlag == "0" ? "正常使用" : "限制使用";
                }
            },
            {field: 'userType', title: '用户类型', align:'center',
                templet:function(d){
                    return d.loginFlag == "0" ? "系统用户" : "普通用户";
                }
            },
            {field: 'createDate', title: '创建时间', align:'center',minWidth:150,sort:true,
                templet:function(d){
                    //处理时间
                    var date = new Date(d.createDate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索用户场景信息
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '/user/query';
            option.method = 'post';
            option.where = {name: searchKey};
            table.render(option);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });


    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var index = layui.layer.open({
                title : "配置用户场景",
                type : 2,
                content : "userSceneEdit?id=" + data.id + "&username=" + data.loginName,
                success : function(layero, index){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回用户场景列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        }
    });

});